<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ page import="archsw0904.controller.catalog.*"%>
<%@ page import="java.math.BigDecimal"%>
<jsp:include page="/partial/process_cache.jsp" />
<jsp:useBean id="usercache" scope="session"
	class="session.cache.UserCache" />
<%
	StringBuffer errorReport = new StringBuffer("");
	Book b = null;

	request.setCharacterEncoding(application
			.getInitParameter("PARAMETER_ENCODING"));

	if (!usercache.isAdmin()) {
		response
				.sendError(401,
						"You have to be an administrator for access at this page");
	}

	if (request.getMethod().compareToIgnoreCase("post") == 0) {
		String isbn = request.getParameter("isbn");
		String author = request.getParameter("author");
		String title = request.getParameter("title");
		String category = request.getParameter("category");
		String tag = request.getParameter("tag");
		String tmpPrice = request.getParameter("price");
		BigDecimal price = null;
		String editor = request.getParameter("editor");
		String tmpCopies = request.getParameter("copies");
		String year = request.getParameter("year");
		Integer copies = null;
		String tmpShopWindow = request.getParameter("shopWindow");
		Boolean shopWindow = false;
		if (tmpShopWindow != null && tmpShopWindow.compareTo("on") == 0) {
			shopWindow = true;
		}
		if (year == null || year.compareTo("") == 0)
			errorReport.append("Field <b>Year</b> can't be empty<br/>");
		else if (year.length() != 4)
			errorReport
					.append("The format if the field <b>Year</b> is not correct <br/>");
		if (isbn == null || isbn.compareTo("") == 0)
			errorReport.append("Field <b>ISBN</b> can't be empty<br/>");
		if (author == null || author.compareTo("") == 0)
			errorReport
					.append("Field <b>Author</b>  can't be empty<br/>");
		if (title == null || title.compareTo("") == 0)
			errorReport
					.append("Field <b>Title</b>  can't be empty<br/>");
		if (category == null || category.compareTo("") == 0)
			errorReport
					.append("Field <b>Category</b>  can't be empty<br/>");
		if (editor == null || editor.compareTo("") == 0)
			errorReport
					.append("Field <b>Editor</b>  can't be empty<br/>");

		try {
			price = new BigDecimal(Double.parseDouble(tmpPrice));
			double pricedb = price.doubleValue();
			price = price.valueOf(pricedb);

		} catch (NumberFormatException e) {
			errorReport
					.append("Field <b>Price</b>  must be a number<br/>");
		} catch (NullPointerException e) {
			errorReport
					.append("Field <b>Price</b>  can't be empty<br/>");
		}
		try {
			copies = new Integer(Integer.parseInt(tmpCopies));
		} catch (NumberFormatException e) {
			errorReport
					.append("Field <b>Copies</b>  must be a number<br/>");
		} catch (NullPointerException e) {
			errorReport
					.append("Field <b>Copies</b>  can't be empty<br/>");
		}

		if ("".compareTo(errorReport.toString()) == 0) {
			CatalogServiceLocator service = new CatalogServiceLocator();
			service.setCatalogPortEndpointAddress(application
					.getInitParameter("CATALOG_WEB_SERVICE_URI"));
			Catalog bookService = service.getCatalogPort();

			b = bookService.createBook(isbn, author, title, category,
					tag, price, editor, copies, year, shopWindow,
					usercache.getToken());
			System.out.println("Dal libro " + b.getAuthor());
			System.out.println("Dal post " + author);

			if (b == null)
				errorReport.append("Error in the book's creation");
		}
	}
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Create Book @ <%=usercache.getHostingNode()%></title>
</head>
<body>
<jsp:include page="/partial/head_menu.jsp" />
<form action="/BookShopWeb/page/add_book.jsp" method="post">
<table>
	<tr>
		<td><b>ISBN *</b></td>
		<td><input type="text" size="30" name="isbn" /></td>
	</tr>
	<tr>
		<td><b>Author *</b></td>
		<td><input type="text" size="30" name="author" /></td>
	</tr>
	<tr>
	<tr>
		<td><b>Title *</b></td>
		<td><input type="text" size="30" name="title" /></td>
	</tr>
	<tr>
		<td><b>Category *</b></td>
		<td><input type="text" size="30" name="category" /></td>
	</tr>
	<tr>
		<td><b>Tag </b></td>
		<td><input type="text" size="30" name="tag" /></td>
	</tr>
	<tr>
		<td><b>Price *</b></td>
		<td><input type="text" size="30" name="price" /></td>
	</tr>
	<tr>
		<td><b>Editor *</b></td>
		<td><input type="text" size="30" name="editor" /></td>
	</tr>
	<tr>
		<td><b>Copies *</b></td>
		<td><input type="text" size="30" name="copies" /></td>
	</tr>
	<tr>
		<td><b>Year *</b></td>
		<td><input type="text" size="30" name="year" /></td>
	</tr>

	<tr>
		<td><b>ShopWindow </b></td>
		<td><input type="checkbox" name="shopWindow" /></td>
	</tr>
	<tr>
		<td colspan="2"><input type="submit" /></td>
	</tr>
</table>
</form>
<%
	if ("".compareTo(errorReport.toString()) != 0) {
%>
<p><font color="red"> <%=errorReport.toString()%> </font></p>
<%
	}
%>
<%
	if (b != null) {
%>
<p><strong>Book created</strong></p>
<p>
<%
	out.println("<p><b>ISBN: " + b.getIsbn() + "</b></p>");
		out.println("<p><b>Author: " + b.getAuthor() + "</b></p>");
		out.println("<p><b>Title: " + b.getTitle() + "</b></p>");
		out.println("<p><b>Category: " + b.getCategory() + "</b></p>");
		out.println("<p><b>Tag: " + b.getTag() + "</b></p>");
		out.println("<p><b>Price: " + b.getPrice() + "</b></p>");
		out.println("<p><b>Editor: " + b.getEditor() + "</b></p>");
		out.println("<p><b>Year: " + b.getYear() + "</b></p>");
		out.println("<p><b>Copies: " + b.getAvailableCopies()
				+ "</b></p>");
%>
</p>
<%
	}
%>
</body>
</html>